


//
//  MBReleaseHouseVC.m
//  发布求租
//
//  Created by MB__Lee on 2018/6/9.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBReleaseHouseVC.h"


//vc
#import "MBRegionSelectVC.h"  //选择区域 --跳转
#import "MBIMSelectedVC.h" // 选择图片 --跳转


// cell
#import "MBQZRegionCell.h" //选择区域
#import "MBHouseBTNCell.h"  //房屋类型
#import "MBZYMCell.h"   //租金佣金房屋面积
#import "MBTianXieCell.h" // 填写信息
#import "MBFloorCell.h"   //楼层

#import "MBCircleBTNCell.h"  //选择类型
#import "MBConentMoreCell.h"  //更多信息
#import "MBTextViewCell.h" //textview
#import "MBTitleCell.h"  // 标题




@interface MBReleaseHouseVC ()<UITableViewDelegate,UITableViewDataSource,MBCircleBTNCellDelegate,MBSeletedCVCDelegate,MBIMSelectedVCDelegate,MBRegionSelectVCDelegate>

@property(nonatomic,strong)UITableView *tableview;

//@property(nonatomic,assign)BOOL isChangeCellState;

@end

static NSString *MBCircleBTNCell_identi = @"MBCircleBTNCell"; // 选择类型--第一个cell，点击可以切换vc
static NSString *MBConentMoreCell_identi = @"MBConentMoreCell";// 点击跳转vc
static NSString *MBTextViewCell_identi = @"MBTextViewCell"; //textView cell
static NSString *MBTitleCell_identi = @"MBTitleCell"; // 标题 内容cell
static NSString *MBResidentCell_identi = @"MBResidentCell"; //房屋户型cell
static NSString *MBHouseBTNCell_identi = @"MBHouseBTNCell"; // 有多个按钮cell
static NSString *MBZYMCell_identi = @"MBZYMCell"; //租金佣金面积cell
static NSString *MBTianXieCell_identi = @"MBTianXieCell"; //填写信息cell
static NSString *MBFloorCell_identi = @"MBFloorCell"; //楼层cell
static NSString *MBSubdaryCell_identi = @"MBSubdaryCell"; //配套cell

@implementation MBReleaseHouseVC
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        
        _tableview.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        
        
         // 选择类型--第一个cell，点击可以切换vc
        [_tableview registerNib:[UINib nibWithNibName:MBCircleBTNCell_identi bundle:nil] forCellReuseIdentifier:MBCircleBTNCell_identi];
        // 点击跳转vc
         [_tableview registerNib:[UINib nibWithNibName:MBConentMoreCell_identi bundle:nil] forCellReuseIdentifier:MBConentMoreCell_identi];
        
        //textView cell
         [_tableview registerNib:[UINib nibWithNibName:MBTextViewCell_identi bundle:nil] forCellReuseIdentifier:MBTextViewCell_identi];
        
        // 标题 内容cell
         [_tableview registerNib:[UINib nibWithNibName:MBTitleCell_identi bundle:nil] forCellReuseIdentifier:MBTitleCell_identi];
        
        //房屋户型cell
         [_tableview registerNib:[UINib nibWithNibName:MBResidentCell_identi bundle:nil] forCellReuseIdentifier:MBResidentCell_identi];
        
        // 有多个按钮cell
         [_tableview registerNib:[UINib nibWithNibName:MBHouseBTNCell_identi bundle:nil] forCellReuseIdentifier:MBHouseBTNCell_identi];
        
        //租金佣金面积cell
         [_tableview registerNib:[UINib nibWithNibName:MBZYMCell_identi bundle:nil] forCellReuseIdentifier:MBZYMCell_identi];
        //填写信息cell
         [_tableview registerNib:[UINib nibWithNibName:MBTianXieCell_identi bundle:nil] forCellReuseIdentifier:MBTianXieCell_identi];
        
        //楼层cell
         [_tableview registerNib:[UINib nibWithNibName:MBFloorCell_identi bundle:nil] forCellReuseIdentifier:MBFloorCell_identi];
        
         //配套cell
         [_tableview registerNib:[UINib nibWithNibName:MBSubdaryCell_identi bundle:nil] forCellReuseIdentifier:MBSubdaryCell_identi];
        
    }
    return  _tableview;
}
-(void)setSourceMut:(NSMutableArray *)sourceMut{
    _sourceMut = sourceMut;
    [self.tableview reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableview];
    
    _houseModel = [[MBReleaseHouseModel alloc] init];
    _houseModel.number = @"";
    _houseModel.token = @"";
    _houseModel.city = MBCityCode;
    
}

#pragma mark ---tableview代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.sourceMut.count - 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MBBaseFBQCell *cell;
    
    //第一行 -- 选择类型
    if (indexPath.row == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:MBCircleBTNCell_identi];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        MBCircleBTNCell *btnCell  = (MBCircleBTNCell *)cell;
        btnCell.selectedIndex = self.selectedIndex;
        
        
        btnCell.BTNModel = self.sourceMut[indexPath.row];
        
        return cell;
    }else if(indexPath.row == 1){
        
        //第二行 -- 填写信息
        cell  = [tableView dequeueReusableCellWithIdentifier:MBTianXieCell_identi];
        cell.CMModel = self.sourceMut[indexPath.row];
        
    }else if (indexPath.row == 2){
        
        //第三行 -- 独家租赁协议
        cell = [tableView dequeueReusableCellWithIdentifier:MBConentMoreCell_identi];
    }else if (indexPath.row == 3){
        
        //第四行 -- 小区名称
        cell = [tableView dequeueReusableCellWithIdentifier:MBTitleCell_identi];
        
    }else if (indexPath.row == 4){
        
        //第五航 -- 区域
        cell = [tableView dequeueReusableCellWithIdentifier:MBConentMoreCell_identi];
        
    }else if (indexPath.row == 5){
        
        //第六行 -- 地址
        cell = [tableView dequeueReusableCellWithIdentifier:MBTitleCell_identi];
    }else if (indexPath.row == 6){
        
        //第七航 -- 小区定位
        cell = [tableView dequeueReusableCellWithIdentifier:MBTitleCell_identi];
    }else if (indexPath.row == 7){
        
        //第八行 --房屋户型
        cell = [tableView dequeueReusableCellWithIdentifier:MBResidentCell_identi];
    }else if (indexPath.row == 8){
        
        //第九行 --房屋面积
        cell = [tableView dequeueReusableCellWithIdentifier:MBZYMCell_identi];
        
    }else if (indexPath.row == 9){
        
        //第十行 -- 楼层
        cell = [tableView dequeueReusableCellWithIdentifier:MBFloorCell_identi];
        
    }else if (indexPath.row == 10){
        
        //第十一行 --朝向
        cell  =[tableView dequeueReusableCellWithIdentifier:MBConentMoreCell_identi];
        
    }else if (indexPath.row == 11){
        
        //第十二行 --装修
        cell = [tableView dequeueReusableCellWithIdentifier:MBHouseBTNCell_identi];
    }else if (indexPath.row == 12){
        
        //第十三行 --配套
        cell = [tableView dequeueReusableCellWithIdentifier:MBSubdaryCell_identi];
        
    }else if (indexPath.row == 13){
        
        //第十四行 -- 租金
        cell = [tableView dequeueReusableCellWithIdentifier:MBZYMCell_identi];
        
    }else if (indexPath.row == 14){
        
        //第十五行 --佣金
        cell = [tableView dequeueReusableCellWithIdentifier:MBZYMCell_identi];
        
    }else if (indexPath.row == 15){
        
        //第十六行 -- 押金方式
        cell = [tableView dequeueReusableCellWithIdentifier:MBHouseBTNCell_identi];
        
        
    }else if (indexPath.row == 16){
        
        //第十七行 -- 付租方式
        cell = [tableView dequeueReusableCellWithIdentifier:MBHouseBTNCell_identi];
        
    }else if (indexPath.row == 17){
        
        //第十八行 -- 特色标签
        cell = [tableView dequeueReusableCellWithIdentifier:MBConentMoreCell_identi];
        
    }else if (indexPath.row == 18){
        
        //第十九行 --标题
        cell = [tableView dequeueReusableCellWithIdentifier:MBTitleCell_identi];
        
    }else if (indexPath.row == 19){
        
        //第二十行 -- 详细描述
        cell = [tableView dequeueReusableCellWithIdentifier:MBTitleCell_identi];
        
    }
    
//    11  装修
//    12  配套
//    15  押金方式
//    16  付租方式
    
    if (indexPath.row == 11 || indexPath.row == 12 || indexPath.row == 15 || indexPath.row == 16) {
        
        if (self.sourceMut.count) {
          cell.BTNModel = self.sourceMut[indexPath.row];
        }
       
    }else{
        
        
        if (self.sourceMut.count) {
             cell.CMModel = self.sourceMut[indexPath.row];
            
            
        }
        
       
        
    }
    
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return  cell;
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    //    11  装修
    //    12  配套
    //    15  押金方式
    //    16  付租方式
    CGFloat hight;
    if (indexPath.row == 0) {
        hight = 94;
    }else if(indexPath.row == 1){
        hight = 90;
    }else if (indexPath.row == 6){
        hight = 160;
    }else if (indexPath.row == 11 || indexPath.row == 12 || indexPath.row == 15 || indexPath.row == 16){
//        MBBaseFBQCell *cell = [[MBBaseFBQCell alloc] init];
        
        NSLog(@"%@",self.sourceMut[indexPath.row]);
        hight =  [MBBaseFBQCell caculateCellHightWithBTNModel:self.sourceMut[indexPath.row]];
        
        if (indexPath.row == 12) {
            hight += 20; //加20
        }
        
    }else if(indexPath.row == 19){
        hight = 214;
    }else{
        hight = 50;
    }
    return  hight;
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    if (_isChangeCellState) {
//        MBBaseFBQCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//        [cell changeStateASSelected];
//        _isChangeCellState = NO;
//        return;
//    }
    
   
    
    if (indexPath.row == 1) {
        
        //第二行 --产权证明
        MBIMSelectedVC *vc  = [[MBIMSelectedVC alloc] init];
        vc.titleName = @"产权证明照片";
        vc.title = @"上传产权证明";
        vc.delegate = self;
        vc.type = MBIMSelectedVCTypeChanQuan;
        
        [self.navigationController pushViewController:vc animated:YES];
    }else if(indexPath.row == 2){
        
        // 第三行 --独家租赁协议
        MBIMSelectedVC *vc  = [[MBIMSelectedVC alloc] init];
        vc.titleName = @"独家租赁委托协议照片";
        vc.title = @"上传租赁协议";
        vc.type = MBIMSelectedVCTypeZuLin;
        vc.delegate = self;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.row == 4){
        
        
        
        //第四行 -- 选择区域
        MBRegionSelectVC *vc  = [[MBRegionSelectVC alloc] init];
        vc.delegate = self;
        [self.navigationController pushViewController:vc animated:YES];
        
        
    }else if(indexPath.row == 10){
        MBSeletedCVC *ortationVC  = [MBCVCFactory MBCVCFactoryCreatWithType:CVCType_houseResidentOritation];
        
        ortationVC.delegate = self;
        [self.navigationController pushViewController:ortationVC animated:YES];
    }else if(indexPath.row == 17){
        MBSeletedCVC * vc  = [MBCVCFactory MBCVCFactoryCreatWithType:CVCType_houseResidentCharacter];
        vc.delegate = self;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    
    
    
}

#pragma mark ------MBIMSelectedVCDelegate --产权证明、租赁协议上传
// row == 1,2
-(void)MBIMSelectedVC:(MBIMSelectedVC *)vc sendImage:(UIImage *)image{
    
 
    
    if (vc.type == MBIMSelectedVCTypeChanQuan) { //产权证明 照片
        
//        _isChangeCellState = YES;
        
        NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:0];
        [self tableView:self.tableview didSelectRowAtIndexPath:index];
        
        
        _houseModel.zhengmingImg = image;
        
        
        
    }else if (vc.type ==MBIMSelectedVCTypeZuLin){ //租赁协议照片
        
      
        
        _houseModel.xieyiImg = image;
        
    }

    
}

#pragma mark --MBRegionSelectVCDelegate --区域   row == 4
-(void)MBRegionSelectVCSendDistrictModel:(ArDistrictModel *)disModel regionModel:(ArRegionModel *)reModel{
    _houseModel.district = disModel.code;
    _houseModel.region = reModel.code;
    
    
      MBConentMoreCell *cell  = (MBConentMoreCell *)[self.tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0]];
    [cell changeStateASSelected];

    MBCommonCellModel *model  = self.sourceMut[4];
    model.xuanze = @"已选择";
    [self.tableview reloadData];
    
}
#pragma mark --MBSeletedCVCDelegate -- 特色标签  row == 10   17
-(void)MBSeletedCVC:(MBSeletedCVC *)selectedVC SendSelectedMut:(NSMutableArray *)mut{
    
    MBConentMoreCell *cell;
    NSIndexPath *indexpath;
    
    if (!mut.count) {
        return;
    }
    if (selectedVC.type == CVCType_houseResidentOritation) { //住宅 朝向
       
            _houseModel.orientation = mut.firstObject;
        
        indexpath = [NSIndexPath indexPathForRow:10 inSection:0];
        
        MBCommonCellModel *model  = self.sourceMut[10];
        model.xuanze = @"选择";
        
    }else if (selectedVC.type == CVCType_houseShopCharater){ //商铺 特色
      
        _houseModel.characteristic = mut;
        
        indexpath = [NSIndexPath indexPathForRow:17 inSection:0];
    }else if(selectedVC.type == CVCType_houseOffBuildingChareater){ // 写字楼 特色
        _houseModel.characteristic = mut;
        
        indexpath = [NSIndexPath indexPathForRow:17 inSection:0];
    }else if (selectedVC.type == CVCType_houseResidentCharacter){//住宅 特色
        _houseModel.characteristic = mut;
    }
   
    cell = (MBConentMoreCell *)[self.tableview cellForRowAtIndexPath:indexpath];
    [cell changeStateASSelected];
    
   
}

@end
